import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_ebook/utils/toast_utils.dart';

class ResponseInterceptor extends Interceptor {
  @override
  void onError(DioException err, ErrorInterceptorHandler handler) {
    String errorMessage = '请检查网络连接';
    if (err.type == DioExceptionType.connectionTimeout ||
        err.type == DioExceptionType.receiveTimeout ||
        err.type == DioExceptionType.sendTimeout) {
      errorMessage = '连接超时,请检查网络连接';
    } else if (err.type == DioExceptionType.receiveTimeout) {
      errorMessage = '请求超时,请检查网络连接';
    } else if (err.response?.statusCode == 404) {
      errorMessage = '请求的资源不存在';
    } else if (err.type == DioExceptionType.unknown) {
      errorMessage = '未知错误,请联系管理员';
    }
    debugPrint(errorMessage);
    //显示错误信息
    ToastUtils.showErrorMsg(errorMessage);
    return handler.next(err);
  }
}
